package com.larry.flume.interceptor;
import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.interceptor.Interceptor;

import java.util.List;
import java.util.Map;

/**
 * @ProjectName hadoop
 * @Author larry
 * @Date 15:43 2021/11/30
 * @Description 自定义拦截器
 **/
public class EventHeaderInterceptor implements Interceptor{

    @Override
    public void initialize() {

    }

    /**
     * 拦截方法
     * @param event
     * @return
     */
    @Override
    public Event intercept(Event event) {
        //1。获取event的header
        Map<String, String> headers = event.getHeaders();
        //2.获取event的body
        String body = new String(event.getBody());

        //判断
        if (body.contains("haha")){
            headers.put("title","ha");
        }else if (body.contains("hehe")){
            headers.put("title","he");
        }
        return event;
    }

    @Override
    public List<Event> intercept(List<Event> list) {

        for (Event event : list) {
            intercept(event);
        }
        return list;
    }

    @Override
    public void close() {

    }

    public static class MyBuilder implements Builder{

        @Override
        public Interceptor build() {
            return new EventHeaderInterceptor();
        }

        @Override
        public void configure(Context context) {

        }
    }
}
